package com.qf.controller;

import com.qf.pojo.Emp;
import com.qf.service.EmpService;
import com.qf.service.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询员工列表
 * @author 千锋健哥
 */
@WebServlet("/empList")
public class EmpListController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 查询员工列表数据
        EmpService empService = new EmpServiceImpl();
        List<Emp> list = empService.findEmpAll();

        //2. 拼接页面
        //resp.setContentType("text/html; charset=utf-8");
        resp.getWriter().println("<!DOCTYPE html>");
        resp.getWriter().println("<html lang=\"en\">");
        resp.getWriter().println("<head>");
        resp.getWriter().println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        resp.getWriter().println("<title>员工列表</title>");
        resp.getWriter().println("</head>");
        resp.getWriter().println("<body>");
        resp.getWriter().println("<a href=\"/add\">添加员工</a>");
        resp.getWriter().println("    <table width=\"800\" border=\"1px solid #ccc\">");
        resp.getWriter().println("      <tr>");
        resp.getWriter().println("        <td>id</td>");
        resp.getWriter().println("        <td>名字</td>");
        resp.getWriter().println("        <td>工资</td>");
        resp.getWriter().println("        <td>年龄</td>");
        resp.getWriter().println("        <td>操作</td>");
        resp.getWriter().println("      </tr>");

        //判断员工列表是否为空, 如果不为空遍历打印
        if (list != null) {
            for (Emp emp : list) {
                resp.getWriter().println("      <tr>");
                resp.getWriter().println("        <td>"+emp.getId()+"</td>");
                resp.getWriter().println("        <td>"+emp.getName()+"</td>");
                resp.getWriter().println("        <td>"+emp.getSalary()+"</td>");
                resp.getWriter().println("        <td>"+emp.getAge()+"</td>");
                resp.getWriter().println("        <td>" +
                        "<a href=\"/update?id="+emp.getId()+"\">修改</a> &nbsp;&nbsp; " +
                        "<a href=\"/delete?id="+emp.getId()+"\">删除</a>" +
                        "</td>");
                resp.getWriter().println("      </tr>");
            }
        }

        resp.getWriter().println("    </table>");
        resp.getWriter().println("</body>");
        resp.getWriter().println("</html>");

    }

}
